from urllib import request
from selenium import webdriver
import urllib.request
import os
import csv
from selenium.webdriver.common.by import By

options = webdriver.EdgeOptions()  # 创建配置项对象
options.add_argument("--disable-blink-features=AutomationControlled")  # 添加配置项:跳过部分反爬虫程序
browser = webdriver.Edge(options=options)  # 将配置项添加到浏览器控制对象中
date = []
browser.get('https://movie.douban.com/top250?start=0')
for i in range(10):
    nums = browser.find_elements(By.XPATH, '//*[@id="content"]/div/div[1]/ol/li')
    for num in nums:
        textname = num.find_element(By.XPATH, './div/div[2]/div[1]/a/span[1]').text
        textfen = num.find_element(By.XPATH, './div/div[2]/div[2]/div/span[2]').text
        textren = num.find_element(By.XPATH, './div/div[2]/div[2]/p[1]').text.split('\n')[0].split('...')[0].replace(
            "/", '')
        textqing = num.find_element(By.XPATH, './div/div[2]/div[2]/p[1]').text.split('\n')[1].replace("/", '')
        date.append([textname, textfen, textren, textqing])
    if i < 9:
        browser.find_element(By.XPATH, '//*[@id="content"]/div/div[1]/div[2]/span[3]/a').click()
with open('dianying.csv', 'w', newline='', encoding='utf-8') as f:
    wher = csv.writer(f)
    wher.writerow(['电影名称', '评分', '演员', '剧情'])
    for i in date:
        wher.writerow(i)
f.close()
